jQuery: What does (function($) {})(jQuery); mean?

Posted by Legend on Stack Overflow See other posts from Stack Overflow or by Legend
Published on 2010-05-30T01:36:57Z Indexed on 2010/05/30 1:42 UTC
Read the original article Hit count: 209

Filed under:
|
|

I am just starting out with writing jQuery plugins. I wrote three small plugins but I have been simply copying the line into all my plugins without actually knowing what it means. Can someone tell me a little more about these?

What does this do? (I know it extends jQuery somehow but is there anything else interesting to know about this)

(function($) {

})(jQuery);

What is the difference between the following two ways of writing a plugin:

Type 1:

(function($) {
    $.fn.jPluginName = {

        },

        $.fn.jPluginName.defaults = {

        }
})(jQuery);

Type 2:

(function($) {
    $.jPluginName = {

        }
})(jQuery);

I could be way off here and maybe both mean the same thing. In some cases, this doesn't seem to be working in a plugin that I was writing using Type 1. Any idea why? But in either case, I would appreciate any explanation.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery